package com.ruoyi.safe.controller.web;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.safe.domain.dto.BizCatalogDetailRelateAttachDTO;
import com.ruoyi.safe.domain.dto.EntityIdsDTO;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.safe.domain.BizCatalogDetail;
import com.ruoyi.safe.service.IBizCatalogDetailService;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 业内资料目录-详情Controller
 * 
 * @author ruoyi
 * @date 2023-12-15
 */
@RestController
@RequestMapping("/detail/detail")
public class BizCatalogDetailController extends BaseController
{
    @Autowired
    private IBizCatalogDetailService bizCatalogDetailService;

    /**
     * 查询业内资料目录-详情列表
     */
    @PreAuthorize("@ss.hasPermi('detail:detail:list')")
    @GetMapping("/list")
    public AjaxResult list(BizCatalogDetail bizCatalogDetail)
    {
//        startPage();
        List<BizCatalogDetail> list = bizCatalogDetailService.selectBizCatalogDetailList(bizCatalogDetail);
        return success(list);
    }

    /**
     * 导出业内资料目录-详情列表
     */
    @PreAuthorize("@ss.hasPermi('detail:detail:export')")
    @Log(title = "业内资料目录-详情", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BizCatalogDetail bizCatalogDetail)
    {
        List<BizCatalogDetail> list = bizCatalogDetailService.selectBizCatalogDetailList(bizCatalogDetail);
        ExcelUtil<BizCatalogDetail> util = new ExcelUtil<BizCatalogDetail>(BizCatalogDetail.class);
        util.exportExcel(response, list, "业内资料目录-详情数据");
    }

    /**
     * 获取业内资料目录-详情详细信息
     */
    @PreAuthorize("@ss.hasPermi('detail:detail:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(bizCatalogDetailService.selectBizCatalogDetailById(id));
    }

    /**
     * 新增业内资料目录-详情
     */
    @PreAuthorize("@ss.hasPermi('detail:detail:add')")
    @Log(title = "业内资料目录-详情", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BizCatalogDetail bizCatalogDetail)
    {
        return toAjax(bizCatalogDetailService.insertBizCatalogDetail(bizCatalogDetail));
    }

    /**
     * 修改业内资料目录-详情
     */
    @PreAuthorize("@ss.hasPermi('detail:detail:edit')")
    @Log(title = "业内资料目录-详情", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizCatalogDetail bizCatalogDetail)
    {
        return toAjax(bizCatalogDetailService.updateBizCatalogDetail(bizCatalogDetail));
    }

    /**
     * 删除业内资料目录-详情
     */
    @PreAuthorize("@ss.hasPermi('detail:detail:remove')")
    @Log(title = "业内资料目录-详情", businessType = BusinessType.DELETE)
	@DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id)
    {
        return toAjax(bizCatalogDetailService.deleteBizCatalogDetailById(id));
    }

    /**
     * 批量删除业内资料目录-详情
     */
    @PreAuthorize("@ss.hasPermi('detail:detail:remove')")
    @Log(title = "业内资料目录-详情", businessType = BusinessType.DELETE)
    @PostMapping("/deleteByIds")
    public AjaxResult deleteByIds(@RequestBody EntityIdsDTO entityIdsDTO)
    {
        if(entityIdsDTO.getIds().length > 0){
            Set idSet = new HashSet<>(Arrays.asList(entityIdsDTO.getIds()));
            return toAjax(bizCatalogDetailService.deleteBizCatalogDetailByIds(entityIdsDTO.getIds()));
        }else{
            throw new ServiceException("id集合不能为空");
        }
    }

    /**
     * 根据目录id查询详情列表
     * @param catalogId
     * @return
     */
    @GetMapping("/catalogId")
    public AjaxResult getListByCatalogId(@RequestParam("catalogId") Long catalogId){
        List<BizCatalogDetail> list = bizCatalogDetailService.selectListByCatalogId(catalogId);
        return AjaxResult.success(list);
    }

    /**
     * 新建下级
     * @param bizCatalogDetail
     * @param id
     * @return
     */
    @PostMapping("/lower")
    public AjaxResult insertLower(@RequestParam Long id,@RequestBody BizCatalogDetail bizCatalogDetail){
        return AjaxResult.success(bizCatalogDetailService.insertLower(id,bizCatalogDetail));
    }

    /**
     * 关联目录详情和附件
     * @param bizCatalogDetailRelateAttachDTO
     * @return
     */
    @PostMapping("/relateAttaches")
    public AjaxResult relateSafeDocAttaches(@RequestBody @Validated BizCatalogDetailRelateAttachDTO bizCatalogDetailRelateAttachDTO){
        return AjaxResult.success(bizCatalogDetailService.relateAttaches(bizCatalogDetailRelateAttachDTO));
    }
}
